﻿using EkStudio.com.bean.xml.BillModule;
using EkStudio.com.bean.xml.ProjectMenu;
using EkStudio.com.bean.xml.reflection.JAXB;
using EkStudio.com.common.unit;
using EkStudio.com.ui.unitbill.property.Event;
using System;
using System.Xml;

namespace EkStudio.com.ui.form
{
    public partial class ModuleSheetSelectForm : DevExpress.XtraEditors.XtraForm
    {

        private log4net.ILog logger = log4net.LogManager.GetLogger(typeof(ModuleSheetSelectForm));

        private bool cRead = false;

        private string modName = "";
        private string modSheet = "";
        

        private string outModKey = "";
        private string outModCaption = "";

        private BillModuleItem outModule = null;


        private ProjectRootPanel RootPanel = null;


        private delegate void UpdateStatusDelegate(string key, string xml);


        public ModuleSheetSelectForm(string cModKey,ProjectRootPanel Root, bool ReadOnly)
        {
            InitializeComponent();
            OutModKey = cModKey;
            RootPanel = Root;
            cRead = ReadOnly;
            InitForm();
        }

        private void InitForm()
        {
            //拆分当前已选择模块及子表
            if("" != OutModKey)
            {
                if (OutModKey.IndexOf(".") > 0)
                {
                    string[] array = OutModKey.Split('.');
                    modName = array[0];
                    if (array.Length > 1)
                    {
                        modSheet = array[1];
                    }
                }
                else
                {
                    modName = OutModKey;
                }
            }
            //加载模块
            BoxItem select = null;
            ModComboBoxEdit.Properties.Items.Clear();
            if(RootPanel.MenuItem!=null&&RootPanel.MenuItem.Business!=null
                && RootPanel.MenuItem.Business.Modules != null)
            {
                foreach(ProjectMenuModGroup group in RootPanel.MenuItem.Business.Modules)
                {
                    if (group.Module != null && group.Module.Count > 0)
                    {
                        foreach(ProjectMenuCommonElement it in group.Module)
                        {
                            
                            BoxItem box = new BoxItem(it.Command, it.Caption,group.Caption);
                            ModComboBoxEdit.Properties.Items.Add(box);
                            if(modName == it.Command)
                            {
                                select = box;
                                ModComboBoxEdit.SelectedItem = box;
                            }
                        }
                    }
                }
            }
            //加载子表
            if (select != null)
            {
                //异步加载模块信息
                bool ReadOnly = false;
                RootPanel.Http.AsyncResourcesXmlBean(select.Key, select.Caption, ProjectUtil.TYPE_BILL_MODULE, ref ReadOnly, null, LoadXml);
                LoadProgressPanel.Visible = true;
            }
            

        }

        /// <summary>
        /// 加载服务器异步回调方法转界面线程 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoadXml(object sender, PropertyDataEvent e)
        {
            //异步转同步
            this.BeginInvoke(new UpdateStatusDelegate(LoadXmlToThreed), new object[] { e.Key, (string)e.Value });

        }

        /// <summary>
        /// xml转bean
        /// </summary>
        /// <param name="key"></param>
        /// <param name="xml"></param>
        private void LoadXmlToThreed(string key, string xml)
        {
            LoadProgressPanel.Visible = false;
            BillModuleItem loadBill = null;
            SheetComboBoxEdit.Properties.Items.Clear();
            SheetComboBoxEdit.SelectedIndex = 0;
            if (key == "OK")
            {
                XmlDocument XmlDoc = new XmlDocument();
                try
                {
                    XmlDoc.LoadXml(xml);
                    JAXBContext<BillModuleItem> jaxb = new JAXBContext<BillModuleItem>();
                    try
                    {
                        loadBill = jaxb.unmarshal(XmlDoc.OuterXml);
                    }
                    catch (Exception ex)
                    {
                        logger.Error("XMLTOBean失败", ex);
                        loadBill = null;
                    }

                }
                catch (Exception ex)
                {
                    logger.Error("返回Xml格式化失败:" + ex.Message, ex);
                    loadBill = null;
                }
            }
            if (loadBill != null)
            {
                outModule = loadBill;
                if (loadBill.UiField != null && loadBill.UiField.FieldsProperties != null)
                {
                    foreach(BillUiGroupItems group in loadBill.UiField.FieldsProperties)
                    {
                        if (group.Table)
                        {
                            BoxItem box = new BoxItem(group.Key, group.Caption,loadBill.getCaption());
                            SheetComboBoxEdit.Properties.Items.Add(box);
                            if(modSheet == group.Key)
                            {
                                SheetComboBoxEdit.SelectedItem = box;
                            }
                        }
                    }
                }
            }
        }

        private void ModComboBoxEdit_EditValueChanged(object sender, EventArgs e)
        {
            BoxItem select = ModComboBoxEdit.SelectedItem as BoxItem;
            //加载子表
            if (select != null)
            {
                //异步加载模块信息
                bool ReadOnly = false;
                RootPanel.Http.AsyncResourcesXmlBean(select.Key, select.Caption, ProjectUtil.TYPE_BILL_MODULE, ref ReadOnly, null, LoadXml);
                LoadProgressPanel.Visible = true;
            }
        }

        private void SubmitBtn_Click(object sender, EventArgs e)
        {
            modName = "";
            modSheet = "";
            OutModKey = "";
            OutModCaption = "";
            if (ModComboBoxEdit.SelectedIndex >= 0)
            {
                BoxItem select = ModComboBoxEdit.SelectedItem as BoxItem;
                modName = select.Key;
                OutModKey = modName;
                OutModCaption = string.Format("[{0}]", select.Caption);
            }
            if (SheetComboBoxEdit.SelectedIndex >= 0)
            {
                BoxItem select = SheetComboBoxEdit.SelectedItem as BoxItem;
                if (select != null)
                {
                    modSheet = select.Key;
                    OutModKey = string.Format("{0}.{1}", OutModKey, modSheet);
                    OutModCaption = string.Format("{0}.[{1}]", OutModCaption, select.Caption);
                }
            }
        }



        public string OutModKey
        {
            get
            {
                return outModKey;
            }

            set
            {
                outModKey = value;
            }
        }

        public string OutModCaption
        {
            get
            {
                return outModCaption;
            }

            set
            {
                outModCaption = value;
            }
        }

        public BillModuleItem OutModule
        {
            get { return outModule; }
            set { outModule = value; }
        }
    }

    class BoxItem
    {
        private string parent = "";
        private string key = "";
        private string caption = "";

        public BoxItem(string _key,string _caption,string _parent)
        {
            key = _key;
            caption = _caption;
            parent = _parent;
        }

        public string Key
        {
            get { return key; }
            set { key = value; }
        }

        public string Caption
        {
            get { return caption; }
            set { caption = value; }
        }


        public override string ToString()
        {
            return string.Format("[{0}].[{1}]", parent, caption);
        }
    }
}